# -*- coding: utf-8 -*-
import scrapy
from douban.items import DoubanItem


class DoubanTop250Spider(scrapy.Spider):
    name = 'douban_top250'
    allowed_domains = ['movie.douban.com']
    start_urls = ['http://movie.douban.com/top250/']

    def parse(self, response):
        divs = response.xpath('//div[@class="item"]')
        item = DoubanItem()
        for div in divs:
            item['ranking'] = div.xpath('.//em/text()').extract_first()
            names = div.xpath('.//span[@class="title"]').extract()
            if len(names) > 1:
                item['name'] = div.xpath('.//span[@class="title"]/text()').extract_first()
                item['foreign_name'] = div.xpath('.//span[@class="title"]/text()').extract()[1]
            else:
                item['name'] = div.xpath('.//span[@class="title"]').extract_first()
                item['foreign_name'] = ''
            item['link'] = div.xpath('.//a[1]/@href').extract_first()
            item['pic'] = div.xpath('.//img/@src').extract_first()
            item['info'] = div.xpath('.//div[@class="bd"]/p/text()').extract_first()
            item['score'] = div.xpath('.//span[@class="rating_num"]/text()').extract_first()
            item['comments'] = div.xpath('.//div[@class="star"]/span[4]/text()').extract_first()
            item['inq'] = div.xpath('.//span[@class="inq"]/text()').extract_first()
            yield item

        next_page = response.xpath('//span[@class="next"]/a/@href').extract_first()
        base_url = 'https://movie.douban.com/top250'
        if next_page:
            # next_page = base_url + next_page
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)
